<?php 
class Http{
	private static $wait_usec = 250000;//0.25 seconds
	public static function request($url,$timeout=30){
		$ch = self::build_curl($url,$timeout);
		$content = curl_exec($ch);
		curl_close($ch);
		return $content;
	}
	private static function build_curl($url,$timeout=30){
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
		return $ch;
		
	}
	public static function request_multi($url_list,$try=3){
		$content_list = array();
		
		$mh = curl_multi_init();
		self::do_request_multi($mh,$url_list,$content_list,$try);
		curl_multi_close($mh);
		
		return $content_list;
	}
	
	public static function do_request_multi($mh,$url_list,&$content_list,$try){
		if($url_list==null || !is_array($url_list) || count($url_list)==0){
			return null;
		}
		
		$ch_list = array();
		foreach($url_list as $url){
			$ch = self::build_curl($url);
			curl_multi_add_handle($mh,$ch);
			$ch_list[$url]=$ch;
		}
		
		//send request
		$active = null;
		do{
			$mrc = curl_multi_exec($mh, $active);
			usleep(self::$wait_usec);
		}while($mrc==CURLM_CALL_MULTI_PERFORM);
		
		while($active && $mrc==CURLM_OK){
			if(curl_multi_select($mh)!=-1){
				do{
					$mrc = curl_multi_exec($mh, $active);
				}while($mrc==CURLM_CALL_MULTI_PERFORM);
			}
		}
		
		//handle response
		foreach ($ch_list as $url=>$ch){
			if(curl_errno($ch)!=0){
				$content_list[$url]=null;
			}else{
				$content_list[$url]=curl_multi_getcontent($ch);
			}
			curl_multi_remove_handle($mh,$ch);
			curl_close($ch);
		}
		
		unset($ch_list);
		
		$retry = $try-1;
		if($retry>0){
			$need_retry=false;
			$retry_url_list=array();
			foreach ($content_list as $url => $content){
				if($content===false || $content==null || $content==''){
					$need_retry = true;
					array_push($retry_url_list, $url);
				}
			}
			if($need_retry){
				$retry_content_list = self::request_multi($mh,$retry_url_list,$content_list,$retry);
				foreach ($retry_content_list as $url=>$content){
					$content_list[$url]=$content;
				}
			}
		}
	}
	
	public static function request_file($url,$dest_dir,$name=null){
		if(!is_dir($dest_dir)){
			mkdir($dest_dir,0777); //若无则创建，并给与777权限 windows忽略
		}
	
		if(!$url || $dest)return false;
		
		$headers = get_headers($url, 1); //得到文件大小
		if ((!array_key_exists("Content-Length", $headers))) {
			$filesize=0;
		}
		if($name!=null){
			$newfname = $dest_dir . name;
		}else{
			$newfname = $dest_dir . basename($url);
		}
		$file = fopen ($url, "rb");
		if ($file) {
			$newf = fopen ($newfname, "wb");
			if ($newf){
				while(!feof($file)) {
					fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
				}
			}
		}
		if($file){
			fclose($file);
		}
		if($newf){
			fclose($newf);
		}
		
		return $newfname;
	}
}
?>